home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: Gihan Perera <Gihan@andante.demon.co.uk>
- Newsgroups: comp.lang.c.moderated,comp.lang.c
- Subject: Re: Please help me elect rounding of int division
- Date: 21 Feb 1996 18:00:09 -0600
- Organization: Rick's Cafe
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4ggbm9$85t@solutions.solon.com>
- References: <4gfaif$1tt@solutions.solon.com>
- Reply-To: Gihan@andante.demon.co.uk
- NNTP-Posting-Host: solutions.solon.com
- X-NNTP-Posting-Host: andante.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.30
- X-Mail2News-Path: andante.demon.co.uk
-
- In article <4gfaif$1tt@solutions.solon.com>
- swedecj@vcnet.com "Carl Jacobson" writes:
-
- > Please help me solve this task.
- >
- > I have now tried (Borland C++ version 3.0) for two solid days to write
- > a function that would allow me to round the quotient of (a/b) up or
- > down based on the "nearest" integer instead of being truncated to the
- > smallest. If exactly half way, return the "odd."
-
- The first part is easy. If you're using reals, add 0.5 to (a/b) before
- truncating it. If you're doing the whole thing with integers, use:
-
- q = ( a + b/2 ) / b;
-
- which has the same effect.
-
- But neither of these meets your second criterion ("return the odd") -
- they both round _up_ if exactly half-way. Sorry - I don't know any
- easy trick for that.
-
- Gihan
-
- -----------------------------------+----------------------------------------
- | Gihan Perera | Me--> * U.K. |
- | | |
- | Email: gihan@andante.demon.co.uk | ,-_|\ |
- | | / \ |
- | | Home--> *_,-._/ |
- | | v |
- -----------------------------------+----------------------------------------
-